﻿Imports System.Data

Partial Class incidencias_ucReporte
    Inherits System.Web.UI.UserControl
    Private objData As DataTable

    Private ls_PageSize As Integer
    Private widestData As Integer = 0

    
    ''' <summary>
    ''' Carga inicial del Reporte
    ''' </summary>
    ''' <param name="pobjDt">DataTable con el resultado de la consulta de búsqueda</param>
    ''' <param name="pOrderBy">Nombre de la columna ordenada por defecto desde la consulta (puede ser vacio "")</param>
    ''' <param name="pStateSort">Orden por defecto de los  resultados de la consulta (ASC o DESC)</param>
    ''' <param name="pPageSize">Numero de Registros por cada página</param>
    ''' <param name="pName">Nombre Único asignado a este reporte</param>
    ''' <param name="pKeyColumn">Numero de Columna que es el Key del resultado (P.E NumID) </param>
    ''' <remarks></remarks>
    Public Sub loadGridView(ByVal pobjDt As DataTable, ByVal pOrderBy As String, ByVal pStateSort As String, ByVal pPageSize As Integer, ByVal pName As String, Optional ByVal pKeyColumn As Integer = 1)

        lblKeyColumn.Text = pKeyColumn
        lblSession.Text = pName
        Session(lblSession.Text) = pobjDt
        objData = pobjDt

        gv_lista.PageSize = pPageSize
        gv_lista.DataSource = pobjDt

        gv_lista.DataBind()
        mostrarNumeroRegistros(1)
        setInitValues(pOrderBy, pStateSort)
        setSortingElements()

    End Sub

    ''' <summary>
    ''' Activa la Opción de Mostrar el detalle de un registro seleccionado en el Formulario Padre
    ''' </summary>
    ''' <param name="pButtonName">ClientId del boton que ejecuta la funcion que muestra el detalle en el formulario padre</param>
    ''' <remarks></remarks>
    Public Sub setButtonDetailControlName(ByVal pButtonName As String)
        hdd_DetailButton.Value = pButtonName
    End Sub

    ''' <summary>
    ''' Activa la opción del doble click en la grilla
    ''' </summary>
    ''' <param name="pType">Tipo de Doble Click que se quiere en la grilla</param>
    ''' <remarks></remarks>
    Public Sub setDoubleClick_Type(ByVal pType As DblClickType)
        hddTypeDoubleClick.Value = pType
    End Sub

    ''' <summary>
    ''' Obtiene el registro seleccionado de la grilla como un DataRow
    ''' </summary>
    ''' <returns>Retorna un DataRow del DataTable</returns>
    ''' <remarks></remarks>
    Public Function getDataRowSelected() As DataRow
        Dim objDt As DataTable = getDataTable()
        If (getSelectedValue() <> "") Then
            Dim dtRow As DataRow() = objDt.Select(objDt.Columns(CType(lblKeyColumn.Text, Integer) - 1).ColumnName.ToString + "='" + getSelectedValue() + "'")
            Return dtRow.First()
        Else
            Return Nothing
        End If
    End Function

    Public Sub initReporte()
        Session(lblSession.Text) = Nothing
        Session("actualSortExpression" + lblSession.Text) = Nothing
        Session("estadoSort" + lblSession.Text) = Nothing
        'postback_page.Style.Add("display", "block")
    End Sub

    ''' <summary>
    ''' Obtiene el DataKey seleccionado 
    ''' </summary>
    ''' <returns>DataKey seleccionado</returns>
    ''' <remarks></remarks>
    Public Function getSelectedValue() As String
        Dim lsRetVal As String = ""
        lsRetVal = hhd_SeletedValue.Value
        Return lsRetVal
    End Function

    ''' <summary>
    ''' Obtiene todo los registros de la consulta
    ''' </summary>
    ''' <returns>DataTable con los resultados de la busqueda</returns>
    ''' <remarks></remarks>
    Public Function getDataTable() As DataTable
        objData = CType(Session(lblSession.Text), DataTable)
        Return objData
    End Function


    Private Sub mostrarNumeroRegistros(ByVal pageIndex As Integer)
        Dim ls_Hasta As String = ""
        Dim ls_Desde As String = ""

        If (objData.Rows.Count < gv_lista.PageSize * pageIndex) Then
            ls_Hasta = objData.Rows.Count.ToString
        Else
            ls_Hasta = (gv_lista.PageSize * pageIndex).ToString
        End If
        ls_Desde = ((gv_lista.PageSize * (pageIndex - 1)) + 1).ToString

        lblRegistros.Text = "Mostrando del " + ls_Desde + " al " + ls_Hasta + " de " + objData.Rows.Count.ToString + " registros"

        If (objData.Rows.Count = 0) Then
            lblRegistros.Text = "No se encontraron resultados."
        End If

    End Sub

    Private Sub setInitValues(ByVal pOrderBy As String, ByVal pEstadoSort As String)
        If (pOrderBy = "") Then
            Session("actualSortExpression" + lblSession.Text) = objData.Columns(0).ColumnName
        Else
            Session("actualSortExpression" + lblSession.Text) = pOrderBy
        End If

        If (pEstadoSort = "") Then
            Session("estadoSort" + lblSession.Text) = pEstadoSort
        Else
            Session("estadoSort" + lblSession.Text) = "ASC"
        End If
    End Sub

    Private Sub setSortingElements()
        For i As Integer = 0 To gv_lista.Columns.Count - 1
            gv_lista.Columns(i).SortExpression = objData.Columns(i).ColumnName
        Next
    End Sub

    Protected Sub gv_lista_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gv_lista.PageIndexChanging
        objData = CType(Session(lblSession.Text), DataTable)
        gv_lista.PageIndex = e.NewPageIndex
        mostrarNumeroRegistros(gv_lista.PageIndex + 1)

        Dim dtView As DataView = New DataView(objData)
        dtView.Sort = Session("actualSortExpression" + lblSession.Text).ToString() + " " + Session("estadoSort" + lblSession.Text).ToString
        Session(lblSession.Text) = dtView.Table
        gv_lista.DataSource = dtView
        gv_lista.DataBind()
    End Sub

    Private Function getSortDirection(ByVal direction As String) As String

        Dim nuevaDireccion As String = String.Empty

        If (direction = "ASC") Then
            nuevaDireccion = "DESC"
        End If

        If (direction = "DESC") Then
            nuevaDireccion = "ASC"
        End If

        Session("estadoSort" + lblSession.Text) = nuevaDireccion

        Return nuevaDireccion
    End Function

    Private Function getOnDblClick(ByVal row As GridViewRow) As String
        Dim type As DblClickType
        type = CType(hddTypeDoubleClick.Value, DblClickType)
        Dim ls_retVal As String = ""
        Select Case type
            Case DblClickType.Redirect_Incidencias

                'FCASTRO - 20111004 : AGREGADO
                'ls_retVal = "jsf_Redirect('frmincidencias.aspx?parm1=" + row.Cells(CType(lblKeyColumn.Text, Integer)).Text + "&parm2=E');"
                ls_retVal = "jsf_Redirect('frmincidencias.aspx?parm1=" + row.Cells(CType(lblKeyColumn.Text, Integer)).Text + "&parm2=E');"
                'FCASTRO - 20111004.

            Case DblClickType.AbrirPopUp1
                ls_retVal = "javascript:return jsf_Abrirpopup('frm_editausuariopopup.aspx?parm=2&parm2=" + row.Cells(CType(lblKeyColumn.Text, Integer)).Text + "','70','100','657','465'); return false;"
            Case DblClickType.ReturnTwoValues

                'FCASTRO - 20111004 : COMENTADO,AGREGADO FUNCION PARA NO DEVOLVER CODIGO HTML
                'ls_retVal = "javascript:return jsf_close2('" + row.Cells(CType(lblKeyColumn.Text, Integer)).Text + "','" + row.Cells(2).Text + "'); return false;"
                ls_retVal = "javascript:return jsf_close2('" + row.Cells(CType(lblKeyColumn.Text, Integer)).Text + "','" + row.Cells(2).Text + "'); return false;"
                'FCASTRO - 20111004.

            Case Else
                ls_retVal = ""
        End Select

        Return ls_retVal
    End Function

    Protected Sub gv_lista_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles gv_lista.Sorting
        objData = CType(Session(lblSession.Text), DataTable)
        Session("actualSortExpression" + lblSession.Text) = e.SortExpression

        If Not (objData Is Nothing) Then
            Dim dtView As DataView = New DataView(objData)
            dtView.Sort = e.SortExpression + " " + getSortDirection(Session("estadoSort" + lblSession.Text).ToString)

            gv_lista.DataSource = dtView
            gv_lista.DataBind()
        End If
    End Sub

    Protected Sub gv_lista_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv_lista.RowDataBound

        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim detail As String = ""
            If (hdd_DetailButton.Value <> "") Then
                detail = "jsf_ClickDetailButton('" + hdd_DetailButton.Value + "');"
            End If

            e.Row.Attributes.Add("OnMouseOver", "Resaltar_On(this);")
            e.Row.Attributes.Add("OnMouseOut", "Resaltar_Off(this);")
            e.Row.Attributes.Add("OnClick", "jsf_SelectRow2('" + gv_lista.ClientID + "',this,'" + e.Row.Cells(CType(lblKeyColumn.Text, Integer)).Text + "','" + hhd_SeletedValue.ClientID + "');" + detail)
            If (hddTypeDoubleClick.Value <> "") Then
                e.Row.Attributes.Add("ondblclick", getOnDblClick(e.Row))
            End If
        End If

        If e.Row.RowType = DataControlRowType.DataRow Or e.Row.RowType = DataControlRowType.Header Then
            For Each cell As TableCell In e.Row.Cells
                cell.Wrap = False
            Next
        End If

    End Sub

End Class

Public Enum DblClickType
    Redirect_Incidencias
    AbrirPopUp1
    ReturnValue
    ReturnTwoValues
    ObtienePerfil
End Enum
